// pages/person/promoterList/index.js
import { debounce } from '../../../../utils/util';
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    list: [],
    pageNo: 1,
    pageSize: 10,
    totalPage: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  getData(isAdd) {
    const { pageNo, pageSize, searchValue } = this.data;
    const params = {
      pageNo,
      pageSize,
      value: searchValue
    };
    $api.getPromoterList(params).then(res => {
      const { list, totalPage } = res.data.data;
      if (isAdd) {
        this.setData({
          list: this.data.list.concat(list)
        });
      } else {
        this.setData({
          list,
          totalPage
        });
      }
    });
  },
  search: debounce(function (e) {
    this.setData({
      searchValue: e.detail
    });
    this.getData();
  }, 500),
  toSelectVillage(e) {
    const userId = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/subcontract/person/pages/selectVillage/index?userId=' + userId
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let { pageNo, totalPage } = this.data;
    if (pageNo == totalPage) return;
    this.setData({
      pageNo: ++pageNo
    });
    this.getData(true);
  },
  /**
   *  页面滚动事件
   */
  onPageScroll(e) {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
